
import bpy
import bmesh
import math
import mathutils




import engine.initLIB.load_dll as engineLib
import ctypes




def f_网格变换_移动(obj, 矩阵):
	me = obj.data

	bm = bmesh.new()
	bm.from_mesh(me)

	bmesh.ops.translate(bm, verts=bm.verts[:], vec=obj.location)
	
	bm.to_mesh(me)
	bm.free()

	obj.location = (0,0,0)




def f_网格变换_旋转(obj, 矩阵):
	me = obj.data

	bm = bmesh.new()
	bm.from_mesh(me)

	bmesh.ops.rotate(bm, verts=bm.verts[:], cent=(1.0, 0.0, 0.0), matrix=mathutils.Matrix.Rotation(math.radians(obj.rotation_euler[0]), 3, 'X'))
	bmesh.ops.rotate(bm, verts=bm.verts[:], cent=(0.0, 1.0, 0.0), matrix=mathutils.Matrix.Rotation(math.radians(obj.rotation_euler[1]), 3, 'Y'))
	bmesh.ops.rotate(bm, verts=bm.verts[:], cent=(0.0, 0.0, 1.0), matrix=mathutils.Matrix.Rotation(math.radians(obj.rotation_euler[2]), 3, 'Z'))

	bm.to_mesh(me)
	bm.free()

	obj.rotation_euler = (0,0,0)




def f_网格变换_缩放(obj, 矩阵):
	me = obj.data

	mat = mathutils.Matrix.Scale(1.0, 4, obj.scale)
	loc = mathutils.Matrix.Translation((0,0,0))
	#mat = loc * mat
	mat[0][0] = obj.scale.x
	mat[1][1] = obj.scale.y
	mat[2][2] = obj.scale.z
	#print('mathutils.Matrix.Scale(1, 4, obj.scale) = ', obj.scale, mat, 矩阵)
	me.transform(mat)
	obj.scale = (1,1,1)
	#me.update_gpu_tag()
	#bm = bmesh.new()
	#bm.from_mesh(me)
	#for v in bm.verts[:]:
	#	v.co.x += 2.0
	#	a = mat * mathutils.Vector(v.co).to_4d()
	#	print('co = ', a)
	#bmesh.ops.scale(bm, verts=bm.verts[:], vec=mathutils.Vector((1.0, 1.0, 1.0)), space=mat)
	#bmesh.ops.scale(bm, verts=bm.verts[:], vec=mathutils.Vector(obj.scale)) #, space=mathutils.Matrix.Scale(1, 4, obj.scale)
	#bm.to_mesh(me)
	#bm.free()
	return
	顶点数据 = []
	me_verts = me.vertices[:]
	for v in me_verts:
		顶点数据.append(tuple(v.co))
	#	v.co.x += 2.0
	engineLib.flexCAPI.f_缩放顶点数据.restype = ctypes.py_object
	顶点数据 = engineLib.flexCAPI.f_缩放顶点数据(ctypes.py_object(顶点数据), ctypes.py_object(tuple(obj.scale)))
	print('顶点数据 = ', 顶点数据)
	index = 0
	for v in me_verts:
		v.co = 顶点数据[index]
	index += 1
	#obj.data = me
	



